package net.thread;

import java.io.File;
import java.io.FileInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

public class ListCallbackDigest implements Runnable
{
	private File input;
	@SuppressWarnings("rawtypes")
	List listenerList = new Vector();
	public ListCallbackDigest(File input)
	{
		this.input = input;
	}
	@SuppressWarnings("unchecked")
	public synchronized void addDigestListener(DigestListener l)
	{
		listenerList.add(l);
	}
	public synchronized void removeDigestListener(DigestListener l)
	{
		listenerList.remove(l);
	}
	@SuppressWarnings("rawtypes")
	public synchronized void sendDigest(byte[] digest)
	{
		ListIterator iterator = listenerList.listIterator();
		while(iterator.hasNext())
		{
			DigestListener dl = (DigestListener) iterator.next();
			dl.digestCalculated(digest);
		}
	}
	@Override
	public void run()
	{
		try
		{
			FileInputStream in = new FileInputStream(input);
			MessageDigest sha = MessageDigest.getInstance("SHA");
			DigestInputStream din = new DigestInputStream(in, sha);
			@SuppressWarnings("unused")
			int b ;
			while((b=din.read())!=-1)
			{
			}
			din.close();
			byte[] digest = sha.digest();
			this.sendDigest(digest);
		}
		catch (Exception e)
		{
		}
	}
	
}
